$.namespace('dckhtjb');

dckhtjb = function() {
	var url = '',isLoaded = false;
	
	return {
		init : function() {
			var myDate = new Date();
			var year = myDate.getFullYear();
			var month = myDate.getMonth() + 1;
			$('#year').combobox('select' , year);
			$('#month').combobox('select' , month);
			EasyUIInit.initControl(this.config.dialog);
		},
		initGrid : function(year,month) {
			$('#grid').datagrid({
				url : ctx + '/rest/grid/dckhtjb/list',
				contentType : 'application/json;charset=utf-8',
				queryParams : {'year':year,'month':month},
				fit : true,
				fitColumns : true,
				rownumbers : true,
				nowrap : true,
				striped : true,
				remoteSort : false,
				singleSelect : true,
				border : false,
				idField : 'id',
				columns : [ [{
					title : '管理队',
					field : 'gridName',
					width : 80,
					formatter : function(value, row, index){
						for(var map in row){
		    			    if(row.hasOwnProperty(map)){
		    			        return map;
		    			    }
		    			}
					}
				}
/*				,{
					title : '单位领导检查',
					field : 'leaderCheck',
					width : 100,
					formatter : function(value, row, index){
						for(var map in row){
		    			    if(row.hasOwnProperty(map)){
		    			        if(row[map].id!=null && row[map].id!=''){
		    			        	return row[map].leaderCheck
		    			        }
		    			    }
		    			}
					}
				}*/
				,{
					title : '数字城管得分',
					field : 'manageCheck',
					width : 100,
					formatter : function(value, row, index){
						for(var map in row){
		    			    if(row.hasOwnProperty(map)){
		    			        if(row[map].id!=null && row[map].id!=''){
		    			        	return row[map].manageCheck
		    			        }
		    			    }
		    			}
					}
				}]],
				toolbar : '#toolbar'
			});
		},
		change : function(){
			var year = $('#year').combobox('getValue');
			var month = $('#month').combobox('getValue');
			dckhtjb.initGrid(year,month);
		},
		showForm : function() {
				
		},
		showEditForm : function(){	
			
			var row = $('#grid').datagrid('getSelected');
			if (!row) {
				$.messager.alert('提示', '请选择要查看的修改', 'info');
				return;
			}
			var id ;
			$('#fm').find('select[id=gridId]').combobox("disable");
			for(var map in row){
			    if(row.hasOwnProperty(map)){
			        if(row[map].id==null ||row[map].id==''){
			        	var year = $('#year').combobox('getValue');
						var month = $('#month').combobox('getValue');
						$('#fmDlg').show().dialog('open');
						$('#fm').form('clear');
						url = ctxRest + '/grid/dckhtjb/create';
						method = 'POST';
						$('#fm').find('select[id=gridId]').combobox("setValue",row[map].gridId);
						$('#fm').find('input[id=year]').textbox('setValue', year);
						$('#fm').find('input[id=month]').textbox('setValue', month);
			        }else{
			        	id = row[map].id;
			        	$('#fmDlg').show().dialog('open');
						url = ctxRest + '/grid/dckhtjb/update';
						$.ajax({
							type: 'GET',
							url : ctxRest + '/grid/dckhtjb/get?id=' + id,
							contentType : 'application/json;charset=utf-8',
							beforeSend : function(){
								progressLoad();
							},
							success : function(result){
								progressClose();//console.log(result);
								$('#fm').form('load', result);

							}
						})
			        }
			    }
			}
			
		},
		saveOrupdate : function(){
			var obj = this.getFormData();
			//console.log(obj);return;
			if (obj){
				progressLoad();
				$.ajax({
					url : url,
					type : 'POST',
					contentType : 'application/json;charset=UTF-8',
					data : JSON.stringify(obj),
					success : function(result){
						if(result == null){
							progressClose();
							$.messager.alert('提示', '该项已存在数据，不需要新增', 'info');
							return;
						}
						progressClose();
						$('#fmDlg').dialog('close');
						$('#grid').datagrid('reload');
					}
				})
			}
		},
		getFormData : function(){
			var fm = $('#fm');
			if (fm.form('validate')) {
				var obj = $.serializeObject(fm);
				return obj;
			}
		},
		
		remove : function(){
			var row = $('#grid').datagrid('getSelected');
			if (!row) {
				$.messager.alert('提示', '请选择要删除的数据', 'info');
				return;
			}
			var id ;
			for(var map in row){
			    if(row.hasOwnProperty(map)){
			        if(row[map]==null || row[map].id==null ||row[map].id==''){
			        	$.messager.alert('提示', '请选择有数据的信息进行删除', 'info');
						return;
			        }else{
			        	id = row[map].id;
			        }
			    }
			}
			$.messager.confirm('提示', '您确定要删除选中的信息？',function(flag){
				if (flag) {
					$.ajax({
						type : 'GET',
						contentType : 'application/json;charset=UTF-8',
						url	: ctxRest + '/grid/dckhtjb/delete?id=' + id,
						beforeSend : function(){progressLoad();},
						success : function(result){
							progressClose();
							if (result && result.errcode == 0) {
								$.messager.alert('提示', '操作成功！', 'info');
								$('#grid').datagrid('reload');
							}
						}
					});
				}
			});
		},
	}
}();

dckhtjb.config = {
		dialog : {
			id : 'fmDlg',
			type : 'easyui-dialog',
			style : 'padding:5px;',
			option : {
				title : '数字城管理考核登记',
				width : 360,
				height : 300,
				closed : true,
				buttons : [
				           {text:'确定',iconCls:'icon-ok',handler:function(){dckhtjb.saveOrupdate();}},
				           {text:'取消',iconCls:'icon-cancel',handler:function(){$('#fmDlg').dialog('close');}}
				           ]
			},
			controls : [
			           {
			        	   id : 'fm',
			        	   type : 'form',
			        	   isExternal : true,
			        	   layout : {
			        		   captionWidth: 108,
			        		   fieldWidth	 : 200,
			        		   fieldHeight : 32,
			        		   rowHeight	 : 38,
			        		   columns	 : 1,
			        		   controls : [ {id:'id',type:'input',isExternal:true,attr:'hidden=true',colspan:0},
			        		                {id :'gridId',caption:'管理队',type : 'easyui-combobox',colspan:2,
							   					option : {
													required : true,
													url : ctx + '/rest/gcb/grid/findList',
													method : 'post',
													textField : 'name',
													valueField : 'id',
													editable : false
												}}, 
			        						{id :'year',caption:'考核年度',type:'easyui-textbox',option : {required : true, editable : false}},
			        						{id :'month',caption:'考核月份',type:'easyui-textbox',option : {required : true, editable : false}},
			        						//{id :'leaderCheck',caption:'单位领导检查分数',type:'easyui-textbox',option : {required : true}},
			        						{id :'manageCheck',caption:'数字城管得分',type:'easyui-textbox',option : {required : true}},
			        					  ]
			        	   }
			           }
			           ]
		},	
		
		
}

$(function() {
	dckhtjb.init();
})



